﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace SignalLab
{
  public class Attached
  {
    #region ToggleButtonAlternate

    public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.RegisterAttached("CheckedContent", typeof(string), typeof(Attached), new PropertyMetadata(null, OnCheckedContentAttachedPropertyChanged));

    public static string GetCheckedContent(DependencyObject obj)
    {
      return (string)obj.GetValue(CheckedContentProperty);
    }
    public static void SetCheckedContent(DependencyObject obj, string value)
    {
      obj.SetValue(CheckedContentProperty, value);
    }

    static void OnCheckedContentAttachedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
      ToggleButton tb = obj as ToggleButton;
      if (tb != null)
      {
        tb.Checked += (sender, arg) =>
        {
          setOriginalContent(tb, tb.Content); //back up original content
          tb.Content = GetCheckedContent(tb); //set alternate content as current Content
        };

        tb.Unchecked += delegate //sets the original content
        {
          tb.Content = getOriginalContent(tb);
          setOriginalContent(tb, null); //remove copy of Content from OriginalContent property
        };
      }
    }
  



    private static object getOriginalContent(DependencyObject obj)
    {
      return (object)obj.GetValue(originalContentProperty);
    }

    private static void setOriginalContent(DependencyObject obj, object value)
    {
      obj.SetValue(originalContentProperty, value);
    }

    private static readonly DependencyProperty originalContentProperty = DependencyProperty.RegisterAttached("originalContent", typeof(object), typeof(Attached), null);

    #endregion

  }
}
